//
//  HomeCollectionView.swift
//  ZHTQWeibo
//
//  Created by ZHTQ on 2018/9/29.
//  Copyright © 2018年 ZHTQ. All rights reserved.
//

import UIKit

class HomeCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate{
    
    /// UICollectionViewFlowLayout
    @IBOutlet weak var collectionViewFlow: UICollectionViewFlowLayout!
    /// collectionHeight
    @IBOutlet weak var collectionViewHeightCon: NSLayoutConstraint!
    /// collectionWidth
    @IBOutlet weak var collectionViewWidthCon: NSLayoutConstraint!
    
    /// 微博数据
    var statusViewModel: StatusViewModel? {
        didSet{
            
            let (imageSize, collectionSize) = countSize()
            // 设置collectionView的布局
            if imageSize != CGSize.zero {
                
                collectionViewFlow.itemSize = imageSize
                
                collectionViewFlow.minimumLineSpacing = 5
                collectionViewFlow.minimumInteritemSpacing = 5
            }
            
            collectionViewWidthCon.constant = collectionSize.width
            collectionViewHeightCon.constant = collectionSize.height
            
            sizeToFit()
            
            reloadData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        dataSource = self
        delegate = self
    }
    
    
    //MARK:- 内部控制方法
    /// 计算图片 和 容器的尺寸
    private func countSize() -> (CGSize, CGSize){
        
        let count = statusViewModel?.thumbnail_urls?.count ?? 0
        
        
        if count == 0 { // 没有图片
            return (CGSize.zero, CGSize.zero)
        }
        
        if count == 1 { // 一张图片
            
            let image = SDWebImageManager.shared().imageCache?.imageFromDiskCache(forKey: statusViewModel?.thumbnail_urls?.last?.absoluteString)
            
            
            return (image!.size, image!.size)
        }
        
        let margen: CGFloat = 5
        
        let imageWidth: CGFloat = (UIScreen.main.bounds.width - 6 * margen) / 3
        let imageHeight: CGFloat = imageWidth
        
        if count == 4 { // 四张图片
            let col = 2
            let row = 2
            let sizeWidth = imageWidth * CGFloat(col) + margen * CGFloat((col - 1))
            let sizeHeight = imageHeight * CGFloat(row) + margen * CGFloat((row - 1))
            
            return (CGSize(width: imageWidth, height: imageHeight), CGSize(width: sizeWidth, height: sizeHeight))
        }
        
        // 其他张数图片
        let col = 3
        let row = (count - 1) / col + 1
        let sizeWidth = imageWidth * CGFloat(col) + margen * CGFloat((col - 1))
        let sizeHeight = imageHeight * CGFloat(row) + margen * CGFloat((row - 1))
        
        return (CGSize(width: imageWidth, height: imageHeight), CGSize(width: sizeWidth, height: sizeHeight))
    }
    
    
    //MARK:- UICollectionViewDataSource
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return statusViewModel?.thumbnail_urls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "pictureCell", for: indexPath) as! HomeCollectionViewCell
        
        cell.url = statusViewModel?.thumbnail_urls![indexPath.item]
        
        return cell
    }
    
    //MARK:- UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        /// 选中微博图片时发送通知 跳转到图片浏览界面
        NotificationCenter.default.post(name: NSNotification.Name(ZHTQCollectionCellSelectedNotification), object: self, userInfo: ["bmiddle_urls" : statusViewModel!.bmiddle_urls!, "indexpath" : indexPath])
    }
}
